package question19;

import jdk.nashorn.internal.ir.debug.JSONWriter;
import netscape.javascript.JSObject;

import java.util.*;

public class question19 {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        String[] split = line.split("-");
        Set<String> positive = new HashSet<>(Arrays.asList(split[0].substring(1, split[0].length() - 1).split(",")));
        Set<String> negative = new HashSet<>(Arrays.asList(split[1].substring(1, split[1].length() - 1).split(",")));
        List<String> report = Arrays.asList(split[2].substring(1, split[2].length() - 1).split(","));

        List<String> student = Arrays.asList(split[3].substring(1, split[3].length() - 1).split(","));
        Integer topK = Integer.valueOf(split[4]);

        PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0];
            }
        });

        for(int i = 0; i < report.size(); i++) {
            String stuReport = report.get(i);
            String stuId = student.get(i);
            int score = 0;
            for(String s : stuReport.split(" ")) {
                if (positive.contains(s)) {
                    score += 3;
                } else if (negative.contains(s)) {
                    score -= 1;
                }
            }
            queue.offer(new int[]{score, Integer.valueOf(stuId)});
        }

        List<Integer> stuId = new ArrayList<>();
        for(int i = 0; i < topK; i++) {
            int[] poll = queue.poll();
            stuId.add(poll[1]);
        }
        String result = "";
        for(Integer id : stuId) {
            result += id + " ";
        }
        System.out.println("[" + result.substring(0, result.length() - 1) + "]");
    }

    public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
        Set<String> positive = new HashSet<>(Arrays.asList(positive_feedback));
        Set<String> negative = new HashSet<>(Arrays.asList(negative_feedback));
        List<String> reportList = Arrays.asList(report);
        PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0];
            }
        });

        for(int i = 0; i < reportList.size(); i++) {
            String stuReport = reportList.get(i);
            int stuId = student_id[i];
            int score = 0;
            for(String s : stuReport.split(" ")) {
                if (positive.contains(s)) {
                    score += 3;
                } else if (negative.contains(s)) {
                    score -= 1;
                }
            }
            queue.offer(new int[]{score, stuId});
        }

        List<Integer> stuId = new ArrayList<>();
        for(int i = 0; i < k; i++) {
            int[] poll = queue.poll();
            stuId.add(poll[1]);
        }
        return stuId;
    }

//    public static void main(String[] args) {
//        List<Integer> integers = new question19().topStudents(new String[]{"smart", "brilliant", "studious"}, new String[]{"not"}, new String[]{"this student is not studious", "the student is smart"}, new int[]{1, 2}, 2);
//        System.out.println(integers.toString());
//    }
}
